<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/10/6
  Time: 18:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="${pageContext.request.contextPath}/">
    <title>添加电影信息</title>
    <style>
        table{
            background-color: darkgray;
        }
    </style>
</head>
<body>
<h1 align="center">添加电影信息</h1>
<form action="AddServlet" method="post">
    <table align="center" border="1px">
        <tr>
            <td>电影名称:</td>
            <td><input type="text" name="movieName" id="movieName"><span id="span1"></span></td>
        </tr>
        <tr>
            <td>电影类型:</td>
            <td><input type="text" name="movieType" id="movieType"><span id="span2"></span></td>
        </tr>
        <tr>
            <td>电影价格:</td>
            <td><input type="text" name="price" id="price"><span id="span3"></span></td>
        </tr>
        <tr>
            <td>是否推荐:</td>
            <td><input type="radio" name="recommend" value="1" checked>推荐<input type="radio" name="recommend" value="0">不推荐</td>
        </tr>
        <tr>
            <td>上映时间:</td>
            <td><input type="text" name="startTime" id="startTime">&nbsp;<span>格式: yyyy-MM-dd</span><span id="span4"></span></td>
        </tr>
        <tr>
            <td>下映时间:</td>
            <td><input type="text" name="endTime" id="endTime">&nbsp;<span>格式: yyyy-MM-dd</span><span id="span5"></span></td>
        </tr>
        <tr>
            <td>投票数:</td>
            <td><input type="text" name="votes" id="votes"><span id="span6"></span></td>
        </tr>
        <tr>
            <td>操作:</td>
            <td><input type="submit" value="添加">&nbsp;<input type="button" onclick="history.back()" value="返回"></td>
        </tr>
    </table>
</form>
<script src="jq/jquery.js"></script>
<script>
    $('form').submit(function () {
        var flag = true;
        $('#span1,#span2,#span3,#span4,#span5,#span6').text('');
        var name = $('#movieName').val();
        if(name==''){
            $('#span1').text('电影名称不能为空！')
            flag = false;
        }else{
            $.ajax({
                "url": "NameServlet",
                "type": "get",
                "data": "movieName="+name,
                "async": false,
                "dataType": "json",
                "success": function(data) {
                    if (data) {
                        $('#span1').text('该电影已经存在了！')
                        flag = false;
                    }
                }
            })
        }
        if($('#movieType').val()==''){
            $('#span2').text('电影类型不能为空')
            flag = false;
        }
        if($('#price').val()==''){
            $('#span3').text('电影价格不能为空')
            flag = false;
        }
        var start = $('#startTime').val();
        var date = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
        if(start==''){
            $('#span4').text('上映时间不能为空')
            flag = false;
        }else if(date.test(start)==false){
            $('#span4').text('时间格式不合法')
            flag = false;
        }
        var end = $('#endTime').val();
        if(end==''){
            $('#span5').text('下映时间不能为空')
            flag = false;
        }else if(date.test(end)==false){
            $('#span5').text('时间格式不合法')
            flag = false;
        }
        if($('#votes').val()==''){
            $('#span6').text('投票数不能为空')
            flag = false;
        }
        return flag;
    })
</script>
</body>
</html>
